mini.parse();

function ArticlesSortHandler() {
};

ArticlesSortHandler.instance = new ArticlesSortHandler();

ArticlesSortHandler.isCollapseOrExpand = true;
ArticlesSortHandler.isQueryBtnExpended = false;

//获得全局的文章分类数据表格对象
ArticlesSortHandler.articlesSortTreeGrid = mini.get("articlesSortGrid");

//加载表格数据
ArticlesSortHandler.articlesSortTreeGrid.load();

/**
 * 根据用户权限展示功能按钮
 */
ArticlesSortHandler.loadToolbar = function() {
	var array = new Array("articles_sort_add", "articles_sort_edit","articles_sort_del","articles_sort_query","articles_sort_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
ArticlesSortHandler.loadToolbar();

/**
 * 弹出添加文章分类窗口
 */
ArticlesSortHandler.addArticlesSort = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(371,230, "添加文章分类");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	var rowData = ArticlesSortHandler.articlesSortTreeGrid.getSelected();
	var selectedId = null;
	var selectedName = null;
	if(rowData != null){
		selectedId = rowData.bagId;
		selectedName = rowData.bagName;
	}

	miniWin.load("articlesSortForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new",bagId: selectedId, bagName: selectedName};
        iframe.contentWindow.ArticlesSortFormHandler.passData(data);
	},function(action){
		ArticlesSortHandler.articlesSortTreeGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑文章分类窗口
 */
ArticlesSortHandler.editArticlesSort = function() {
	var rowData = ArticlesSortHandler.articlesSortTreeGrid.getSelected();
	var rowsData = ArticlesSortHandler.articlesSortTreeGrid.getSelecteds();
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(371, 230, "编辑文章分类[<font color='blue'>" + rowData.bagName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("articlesSortForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bagId: rowData.bagId};
            iframe.contentWindow.ArticlesSortFormHandler.passData(data);
		},function(action){
			ArticlesSortHandler.articlesSortTreeGrid.reload();
		});
		
		miniWin.show();
	}else if(rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除文章分类资源
 */
ArticlesSortHandler.delArticlesSort = function() {
	var rowsData = ArticlesSortHandler.articlesSortTreeGrid.getSelecteds();
	
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bagId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bagId;
					
					// 判断当前文章分类或其子分类是否被使用
					if(ArticlesSortHandler.getBaiByBas(ids)){
						MiniuiTools.miniWarning("系统提示", "选择的记录存在文章信息关联，请检查后重试");
						return;	
					}
					
					ArticlesSortHandler.articlesSortTreeGrid.loading();
					
                    $.ajax({
                        url: __root + "/delBagByIds.do",
                        data: {
				        	bagIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		ArticlesSortHandler.articlesSortTreeGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	ArticlesSortHandler.articlesSortTreeGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 检验选择的记录是否已被文章使用
 * @param {} bsiIds
 * @return {}
 */
ArticlesSortHandler.getBaiByBas = function(bagIds){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBaiByBas.do?bagIds=" +bagIds,
        dataType: 'json',
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 更新文章分类状态
 */
ArticlesSortHandler.updateBagState = function() {
	if (!MiniuiTools.hasPurview("articles_sort_edit")) {
		return;
	}
	var rowData = ArticlesSortHandler.articlesSortTreeGrid.getSelected();
	var bagId = rowData.bagId;
	
	ArticlesSortHandler.articlesSortTreeGrid.loading();
    $.ajax({
        url: __root + "/updateBasStateById.do",
        data: {
        	bagId: bagId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            ArticlesSortHandler.articlesSortTreeGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	ArticlesSortHandler.articlesSortTreeGrid.unmask();
        }
    });	
};

/**
 * 展开或折叠查询文章分类面板
 */
ArticlesSortHandler.queryArticlesSort = function() {
	if (ArticlesSortHandler.isQueryBtnExpended) {
		ArticlesSortHandler.isQueryBtnExpended = false;
		mini.get("articles_sort_query").setText("展开查询");
		mini.get("articles_sort_query").setIconCls("icon-btn-expand");
	} else {
		ArticlesSortHandler.isQueryBtnExpended = true;
		mini.get("articles_sort_query").setText("折叠查询");
		mini.get("articles_sort_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryArticlesSortDiv").toggle(500);
    setTimeout(function(){mini.get("articlesSortPanel").doLayout();},600);
};

/**
 * 执行查询文章分类的方法
 */
ArticlesSortHandler.doQueryArticlesSort = function(){
	var bagName = mini.get("query_bagName").getValue();
	
    ArticlesSortHandler.articlesSortTreeGrid.load({bagName: bagName});
};

/**
 * 刷新页面数据及布局
 */
ArticlesSortHandler.afresh = function(){
	ArticlesSortHandler.doQueryArticlesSort();
	mini.get("articlesSortPanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
ArticlesSortHandler.articlesSortTreeGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bagState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='ArticlesSortHandler.updateBagState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='ArticlesSortHandler.updateBagState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
});

